.NET Compaction - Introduce preconfigured CompactionStrategy pattern#4665
.NET Compaction - Introduce preconfigured CompactionStrategy pattern#4665
CompactionStrategy pattern#4665Conversation
…eate() factory Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>
CompactionStrategy pattern
There was a problem hiding this comment.
Pull request overview
This PR introduces a preconfigured compaction “menu” for the .NET Agent Framework, adding a CompactionStrategy.Create(approach, size, chatClient?) factory that maps high-level options to a tuned PipelineCompactionStrategy.
Changes:
- Added
CompactionApproachandCompactionSizeenums to represent the two configuration axes. - Implemented
CompactionStrategy.Create(...)factory to generate Gentle/Balanced/Aggressive pipelines with staged triggers. - Added unit tests validating pipeline composition, argument validation, and (partially) size behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.cs | Marks CompactionStrategy as partial to allow adding the factory in a separate file. |
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.Factory.cs | Adds CompactionStrategy.Create(...) and internal size→threshold mappings for pipeline construction. |
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionApproach.cs | Adds the CompactionApproach enum describing pipeline composition choices. |
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionSize.cs | Adds the CompactionSize enum describing context-size profiles. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionStrategyCreateTests.cs | Adds unit tests for the new factory method and size differentiation behavior. |
dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionStrategyCreateTests.cs
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionStrategyCreateTests.cs
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionStrategyCreateTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| /// </summary> | ||
| /// <seealso cref="CompactionStrategy.Create"/> | ||
| [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] | ||
| public enum CompactionApproach |
There was a problem hiding this comment.
Does the terms used here, e.g. "Gentle", "lightest", or "Aggressive" refer to the costliness (both time and resources) of the techniques used?
If so, would some reference to cost be appropriate, e.g. "Fast", "Balanced", "Slow" or "Cheap" , "Balanced", "Expensive"?
After all, I wouldn't necessarily consider Truncation a gentle technique. It's rather blunt and aggressive IMO 😄
| /// </remarks> | ||
| /// <seealso cref="CompactionStrategy.Create"/> | ||
| [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] | ||
| public enum CompactionSize |
There was a problem hiding this comment.
Should we explain what Compact, Moderate and Generous means here in actual numbers, so that users can more easily make a decision about which one fits their model?
Also, how does the chosen numbers match actual models in the real world. Do they align well with the maximum context window sizes typical of popular models out there?
Developers shouldn't need to understand internal strategy parameters to get a well-tuned compaction pipeline. This adds a
CompactionStrategy.Create(approach, size, chatClient?)factory that maps two intuitive axes to a pre-configuredPipelineCompactionStrategy.New types
CompactionApproach— controls pipeline composition:Gentle: tool-result collapsing + truncation backstop (no LLM required)Balanced: tool-result collapsing + LLM summarization + truncation backstopAggressive: tool-result collapsing + LLM summarization + sliding window + truncationCompactionSize— scales all trigger thresholds to match model context capacity:Compact≈ 8 K tokens,Moderate≈ 32 K tokens,Generous≈ 64 K tokensFactory logic
Within each pipeline, early stages trigger at a fraction of the size limit (⅔ for
Balanced, ½ forAggressive) so incremental compaction fires before reaching the emergency truncation backstop.Usage
chatClientis required (and validated) only whenapproachisBalancedorAggressive.Contribution Checklist
Original prompt
CompactionStrategypattern #4603💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.